package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.adapter.MyMovieAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.MyCollectBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MyCollectionActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private TextView mMyCollectionEdit,mMyCollectionAll,mMyCollectionDelete;
    private LinearLayout mMyCollectionBottom;
    private MyMovieAdapter adapter;
    private int index = 0;
    private int indexAll = 0;
    private SharedPreferencesHelper sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycollection);
        sp = new SharedPreferencesHelper(this,"logindata");
        setTitleWithBack();
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.mycollect_rv);
        mMyCollectionEdit = findViewById(R.id.mycollect_tv_edit);
        mMyCollectionAll = findViewById(R.id.mycollect_tv_all);
        mMyCollectionDelete = findViewById(R.id.mycollect_tv_delete);
        mMyCollectionBottom = findViewById(R.id.mycollect_linear_bottom);

        adapter = new MyMovieAdapter(this);
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(10,3));
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setAdapter(adapter);

        mMyCollectionEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (index == 0){
                    mMyCollectionEdit.setText("取消");
                    mMyCollectionBottom.setVisibility(View.VISIBLE);
                    index = 1;
                    adapter.setIndexVis(1);
                }else {
                    mMyCollectionEdit.setText("编辑");
                    mMyCollectionBottom.setVisibility(View.GONE);
                    index = 0;
                    adapter.setIndexVis(0);
                }
            }
        });

        mMyCollectionAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (indexAll == 0){
                    adapter.setAllSelect();
                    mMyCollectionAll.setText("取消全选");
                    indexAll = 1;
                }else {
                    adapter.closeAllSelect();
                    mMyCollectionAll.setText("全选");
                    indexAll = 0;
                }

                mMyCollectionDelete.setText("删除("+adapter.getSelectedItem().size()+")");
            }
        });

        adapter.setOnClickListener(new MyMovieAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                mMyCollectionDelete.setText("删除("+adapter.getSelectedItem().size()+")");
            }
        });

        mMyCollectionDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteCollect();
            }
        });

        initData();
    }

    private void initData(){
        RequestUtil.getInstence().API().favorites((String) sp.getSharedPreference("token",""))
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        MyCollectBean bean = gson.fromJson(t,MyCollectBean.class);

                        adapter.clearList();
                        adapter.addList(bean.getData());
                    }

                    @Override
                    protected void onFail(int error_code) {
                        adapter.clearList();
                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }


    private void deleteCollect(){
        if (adapter.getSelectedItem().size() <= 0){
            Toast.makeText(this,"请选择删除项目",Toast.LENGTH_LONG).show();
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0;i < adapter.getSelectedItem().size();i++){
            if (i == (adapter.getSelectedItem().size() - 1)){
                stringBuilder.append(adapter.getSelectedItem().get(i).getId());
            }else {
                stringBuilder.append(adapter.getSelectedItem().get(i).getId()+",");
            }
        }
        Log.e("TAG","id:"+stringBuilder.toString());
        RequestUtil.getInstence().API().favorites_remove((String) sp.getSharedPreference("token",""),stringBuilder.toString())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        mMyCollectionEdit.setText("编辑");
                        mMyCollectionBottom.setVisibility(View.GONE);
                        adapter.setIndexVis(0);
                        index = 0;
                        indexAll = 0;
                        mMyCollectionAll.setText("全选");
                        mMyCollectionDelete.setText("删除(0)");
                        initData();
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
